package com.yangoxix.lovegoing.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yangoxix.lovegoing.mapper.AuditUserMapper;
import com.yangoxix.lovegoing.mapper.ImageMapper;
import com.yangoxix.lovegoing.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * The type Audit service.
 *
 * @author chenzou
 * @date 2021 -09-18 10:47:01
 */
@Service
public class AuditService {


    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private AuditUserMapper auditUserMapper;

    /**
     * Audit pass.对活动进行审核的service
     *
     * @param named the named
     * @author chenzou
     * @date 2021 -09-18 10:47:01
     */
    public boolean auditPass(String named) {
        String auditName="未审核|"+named;

        UpdateWrapper<Image> wra = new UpdateWrapper<Image>().eq("activityname", auditName).set("activityname",named);

        int result = imageMapper.update(null, wra);

//        获取审查表单中的数据
        AuditActivity auditActivity = new AuditActivity();

        AuditActivity findedAct = auditActivity.selectById(named);

        if (findedAct == null) {
            return false;
        }

        boolean del = auditActivity.deleteById(findedAct.getActivityname());

// 序列化成json 在序列化成activity

        JSON json = (JSON) JSONObject.toJSON(findedAct);

        Activity activity = JSONObject.toJavaObject(json, Activity.class);

        boolean insert = activity.insert();

        return insert&&del;
    }

    /**
     * Activity join user string.加入审核用户
     *
     * @param activityname the activityname
     * @param username     the username
     * @return the string
     * @author chenzou
     * @date 2021 -09-18 10:47:01
     */
    public String activityJoinUser(String activityname, String username) {
        AuditUser auditUser = new AuditUser();
        QueryWrapper<AuditUser> eq = new QueryWrapper<AuditUser>().eq("activityname", activityname).eq("username", username);
        AuditUser auditUser1 = auditUser.selectOne(eq);
//      从用户报名表中获取是否用户已经报名成功
        UserInActivity userIn = new UserInActivity();
        QueryWrapper<UserInActivity> wra = new QueryWrapper<>();
        wra.eq("activityname", activityname).eq("username", username);
        UserInActivity userInActivity = userIn.selectOne(wra);

        if (auditUser1 == null&&userInActivity==null) {
//            用户审批表没有该用户，故用户没有报名活动可以报名
//            对活动人数进行后端逻辑判断，，，，，，，，后面在弄
            auditUser.setUsername(username);
            auditUser.setActivityname(activityname);
            auditUser.insert();
            return null;
        } else {
//            用户不可以报名，返回错误信息
            return "请勿重复报名";
        }
    }

    /**
     * Audit pass user.对用户进行审核的service
     *
     * @param name         the name
     * @param activityname the activityname
     * @author chenzou
     * @date 2021 -09-18 10:47:01
     */
    public void auditPassUser(String name, String activityname) {


        AuditUser auditUser = new AuditUser();

        QueryWrapper<AuditUser> eq = new QueryWrapper<AuditUser>().eq("activityname", activityname).eq("username", name);

        AuditUser finded = auditUser.selectOne(eq);

        int delete = auditUserMapper.delete(eq);

        //        序列化成json 在序列化成activity

        JSON json = (JSON) JSONObject.toJSON(finded);

        UserInActivity userInActivity = JSONObject.toJavaObject(json, UserInActivity.class);

        userInActivity.insert();

    }

    /**
     * Audit unpass user.通过活动名和用户名把用户从活动的审核表中删除
     *
     * @param name         the name用户名
     * @param activityname the activityname活动名
     * @author chenzou
     * @date 2021 -09-19 16:30:52
     */
    public void auditUnpassUser(String name, String activityname){
        AuditUser auditUser = new AuditUser();
        QueryWrapper<AuditUser> eq = new QueryWrapper<AuditUser>().eq("activityname", activityname).eq("username", name);
        int delete = auditUserMapper.delete(eq);
    }

    /**
     * Get all audit act list.获取全部待审核的活动
     *
     * @return the list
     */
    public List<AuditActivity> getAllAuditAct(){
        AuditActivity auditActivity = new AuditActivity();
        List<AuditActivity> auditActivities = auditActivity.selectList(null);
        return auditActivities;
    }

    /**
     * Audit fail boolean.   审核失败
     *
     * @param name the name
     * @return the boolean
     */
    public boolean auditFail(String name) {
        AuditActivity auditActivity = new AuditActivity();
       return auditActivity.deleteById(name);

    }

    public List<AuditPublisher> getAllPublisherApplyed() {
        AuditPublisher auditPublisher = new AuditPublisher();
        return auditPublisher.selectList(null);

    }
}
